package DBLayer;
import ModelLayer.*;

import java.sql.*;
import java.util.ArrayList;


public class DBClothing implements IFDBGeneric<Clothing>{
    private  Connection con;
   
    public DBClothing() {
      con = DbConnection.getInstance().getDBcon();
    }
    

    public ArrayList<Clothing> getAll(){
        return miscWhere("");
    }
    
    public Clothing find(String name){  
        return singleWhere("name = '"+name);
    }
    
    public Clothing find_byID(int Prid){  
        return singleWhere("Prid = '"+Prid);
    }
   
    
    public int insert(Clothing clo) throws Exception
    {  
  
       int rc = -1;
	   String query="INSERT INTO Product(name, PurchasePrice, SalesPrice, RentPrice, CountryOfOrigin, CurrentStock, MinStock, Sid)  VALUES('"+
	  		clo.getName()  + "',"  +
		    clo.getPurchasePrice()  + ","  +
            clo.getSalesPrice() + "," +
            clo.getRentPrice() + ",'" +
            clo.getCountryOfOrigin() + "',"+ clo.getCurrentStock() + ","+ clo.getMinStock() + ","+clo.getSid() + ")" ;
	   
	   		


      try{
          Statement stmt = con.createStatement();
          stmt.setQueryTimeout(5);
     	  rc = stmt.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
     	  ResultSet rs = stmt.getGeneratedKeys();
     	  if (rs.next()) rc = rs.getInt(1);
     	
     			  
     	String query1 = "INSERT INTO Clothing(Prid, colour, size) VALUES("+rc+","
     			+ " '"+clo.getColour()+"','"+clo.getSize()+"')";
     	stmt.executeUpdate(query1);
          stmt.close();
      }
       catch(SQLException ex){
          System.err.println("Could not insert clothing obj with name "+clo.getName());
       }
       return(rc);
    }

    
	public int update(Clothing clo)
	{
	Clothing cloObj  = clo;
		int rc=-1;

		String query="UPDATE Product SET "+
		 	  "name ='"+ cloObj.getName()+"', "+
		 	  "purchasePrice ="+ cloObj.getPurchasePrice() + ", " +
              "salesPrice ="+ cloObj.getSalesPrice() + ", " +
              "rentPrice ="+ cloObj.getRentPrice() + ", " +
              "countryOfOrigin ='"+ cloObj.getCountryOfOrigin() + "', " +
              "currentStock ="+ cloObj.getCurrentStock() + ", " +
              "minStock ="+ cloObj.getMinStock() + " " +
		          " WHERE Prid = "+ cloObj.getPrid() + ""; 
  		try{ 
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		System.out.println(query);
	 	 	rc = stmt.executeUpdate(query);
	 	 	
	 	String query1="UPDATE Clothing SET "+
	 			"colour ='"+ cloObj.getColour()+"',"+
	 			"size = '"+ cloObj.getSize()+"' "+
	 				"WHERE Prid = "+cloObj.getPrid() + "";
	 		stmt.executeUpdate(query1);
	 	 	
	 	 	
	 	 	

	 	 	stmt.close();
		}
	 	catch(Exception ex){
	 	 	System.out.println("Update exception in clothing db: "+ex);
	  	}
		return(rc);
	}
	
	
	public int delete(int Prid)
	{
        int rc=-1;
	  
	  	String query="DELETE FROM product WHERE Prid = '" +Prid + "'";
       
	  	try{ 
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 	  	rc = stmt.executeUpdate(query);
	 	  	stmt.close();
  		}
   	        catch(Exception ex){
	 	  	System.out.println("Delete exception in clothing db: "+ex);
   	        }
		return(rc);
	}
	

	 
	private ArrayList<Clothing> miscWhere(String wClause)
	{
            ResultSet results;
	    ArrayList<Clothing> list = new ArrayList<Clothing>();	
		
	    String query =  buildQuery(wClause);
  
            try{ 
		Statement stmt = con.createStatement();
	 	stmt.setQueryTimeout(5);
	 	results = stmt.executeQuery(query);
	 	
	
		while( results.next() ){
	     	 Clothing cloObj = new Clothing();
	     	 cloObj = buildClothing(results);	
                 list.add(cloObj);	
		}
   
			
		}
	 	catch(Exception e){
	 		System.out.println("Query exception - select: "+e);
	 	}
		return list;
	}
	
		
	private Clothing singleWhere(String wClause)
	{
		ResultSet results;
		Clothing cloObj = new Clothing();
                
	        String query =  buildQuery(wClause);
		try{ // read the product from the database
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() ){
                            cloObj = buildClothing(results);
                          
                            stmt.close();
                           
			}
                        else{
                            cloObj = null;
                        }
		}
	 	catch(Exception e){
	 		System.out.println("Query exception: "+e);
	 	}
		return cloObj;
	}
	
	private String buildQuery(String wClause)
	{
	    String query="SELECT Prid, name, purchasePrice,SalesPrice, rentPrice, CountryOfOrigin,currentStock, minStock, Sid, Colour, size FROM Clothing c, Product p WHERE c.Prid=p.Prid";
		
		if (wClause.length()>0)
			query=query+" AND "+ wClause;
			
		return query;
	}
	
	private Clothing buildClothing(ResultSet results)
      {   Clothing cloObj = new Clothing();
          try{
        	  	cloObj.setPrid(results.getInt("Prid"));
                cloObj.setName(results.getString("name"));
                cloObj.setPurchasePrice(results.getDouble("purchasePrice"));
                cloObj.setSalesPrice(results.getDouble("salesPrice"));
                cloObj.setRentPrice(results.getDouble("rentPrice"));
                cloObj.setCountryOfOrigin(results.getString("CountryOfOrigin"));
                cloObj.setCurrentStock(results.getInt("currentStock"));
                cloObj.setMinStock(results.getInt("minStock"));
                cloObj.setSid(results.getInt("Sid"));
                cloObj.setColour(results.getString("colour"));
                cloObj.setSize(results.getString("size"));

          }
         catch(Exception e)
         {
             System.out.println("error in building the clothing object");
         }
         return cloObj;
      }

 
}  
    

